package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

/**
 * @description: 获取AccessToken
 * @author: Hupengpeng
 * @time: 2021/7/3 9:46
 */
public class OauthAccessToken {
    private static final Log log = LogFactory.get();
    private String client_key;
    private String client_secret;
    private String code;
    private String grant_type;
    private String response;

    private OauthAccessToken() {
    }

    public String getClient_key() {
        return client_key;
    }

    public OauthAccessToken setClient_key(String client_key) {
        this.client_key = client_key;
        return this;
    }

    public String getClient_secret() {
        return client_secret;
    }

    public OauthAccessToken setClient_secret(String client_secret) {
        this.client_secret = client_secret;
        return this;
    }

    public String getCode() {
        if (StrUtil.isEmpty(code)) {
            throw new DouYinException("code 授权码 不能为空！");
        }
        return code;
    }

    public OauthAccessToken setCode(String code) {
        this.code = code;
        return this;
    }

    public String getGrant_type() {
        if (StrUtil.isEmpty(grant_type)) {
            grant_type = "authorization_code";
        }
        return grant_type;
    }

    public OauthAccessToken setGrant_type(String grant_type) {
        this.grant_type = grant_type;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public OauthAccessToken setResponse(String response) {
        this.response = response;
        return this;
    }

    protected static OauthAccessToken New() {
        return new OauthAccessToken();
    }

    public OauthAccessToken execute() {
        String url = StrUtil.format("{}{}?grant_type={}&client_key={}&client_secret={}&code={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_OAUTH_ACCESS_TOKEN,
                getGrant_type(),
                getClient_key(),
                getClient_secret(),
                getCode());
        String rs = HttpUtil.get(url);
        response = rs;
        return this;
    }

    public OauthAccessTokenResponse convert() {
        JSONObject obj = JSONUtil.parseObj(response);
        if ("success".equalsIgnoreCase(obj.getStr("message"))) {
            return JSONUtil.toBean(obj.getJSONObject("data"), OauthAccessTokenResponse.class);
        }
        throw new DouYinException(response);
    }

    public class OauthAccessTokenResponse {
        private String access_token;//接口调用凭证
        private String captcha;//
        private String desc_url;
        private String description;//错误码描述
        private int error_code;//错误码
        private int expires_in;//access_token接口调用凭证超时时间，单位（秒)
        private String open_id;//授权用户唯一标识
        private int refresh_expires_in;//refresh_token凭证超时时间，单位（秒)
        private String refresh_token;//用户刷新access_token
        private String scope;//用户授权的作用域(Scope)，使用逗号（,）分隔，开放平台几乎几乎每个接口都需要特定的Scope。

        public String getAccess_token() {
            return access_token;
        }

        public void setAccess_token(String access_token) {
            this.access_token = access_token;
        }

        public String getCaptcha() {
            return captcha;
        }

        public void setCaptcha(String captcha) {
            this.captcha = captcha;
        }

        public String getDesc_url() {
            return desc_url;
        }

        public void setDesc_url(String desc_url) {
            this.desc_url = desc_url;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public int getError_code() {
            return error_code;
        }

        public void setError_code(int error_code) {
            this.error_code = error_code;
        }

        public int getExpires_in() {
            return expires_in;
        }

        public void setExpires_in(int expires_in) {
            this.expires_in = expires_in;
        }

        public String getOpen_id() {
            return open_id;
        }

        public void setOpen_id(String open_id) {
            this.open_id = open_id;
        }

        public int getRefresh_expires_in() {
            return refresh_expires_in;
        }

        public void setRefresh_expires_in(int refresh_expires_in) {
            this.refresh_expires_in = refresh_expires_in;
        }

        public String getRefresh_token() {
            return refresh_token;
        }

        public void setRefresh_token(String refresh_token) {
            this.refresh_token = refresh_token;
        }

        public String getScope() {
            return scope;
        }

        public void setScope(String scope) {
            this.scope = scope;
        }
    }
}
